﻿package fwk.loaderManager {	
	
	import flash.events.EventDispatcher;
	import fwk.loaderManager.events.*;
	
	/**
	* Object décrivant une ressource à charger
	*/
	public class LoaderItemObject extends EventDispatcher
	{
		/**
		 * A unique id for the LoaderItemObject
		 */
		public var id:String;
		/**
		 * The url of the object to load
		 */
		public var url:String;
		/**
		* (optional) The type of the object to load. If no set, the type is determinated with the file extension. The valid type are enumerated in LoaderItemType
		*/
		public var type:String;
		/**
		* Indicate if the object to load will to load in the context of the application. Default true.
		*/
		public var context:Boolean = true;
		/**
		* Indicate if the object to load will to load in the security domain of the application. Default false.
		*/
		public var securityDomain:Boolean = false;
		/**
		* (optional) A label for the LoaderItemObject. Default null
		*/
		public var label:String;
		
		private var _isCompleted:Boolean;
		private var _content:Object;
		private var _percent:Number ;
		
		private var _bytesLoaded:Number ;
		private var _bytesTotal:Number ;
		private var _cache:Boolean = false;
		
		/**
		 * The bytes loaded of the object
		 * @param val A number of bytes loaded
		 */
		public function set bytesLoaded(val:Number):void
		{
			_bytesLoaded = val;
		}
		/**
		 * The total bytes of the object
		 * @param val A number of total bytes of the object
		 */
		public function set bytesTotal(val:Number):void
		{
			_bytesTotal = val;
		}
		/**
		 * The bytes loaded of the object
		 * @return A number of bytes loaded
		 */
		public function get bytesLoaded():Number
		{
			return _bytesLoaded;
		}
		
		/**
		 * The total bytes of the object
		 * @return A number of total bytes of the object
		 */
		public function get bytesTotal():Number
		{
			return _bytesTotal;
		}
		
		/**
		* The percent loaded of the LoaderItemObject
		* @param  val A value between 0 and 1
		 */
		public function set percent(val:Number):void
		{
			_percent = val;
			
			// déclenchement event
			var evt:LoaderEvent = new LoaderEvent(LoaderEvent.PROGRESS);
			evt.percent = _percent;
			this.dispatchEvent(evt);
		}
		/**
		* The percent loaded of the LoaderItemObject
		* @return A value between 0 and 1
		 */
		public function get percent():Number
		{
			return _percent;
		}
		/**
		*  Indicate if the LaoderItemObject is complete or not
		*  @return true or false
		*/
		public function get completed():Boolean
		{
			return _isCompleted;
		}
		/**
		*  Indicate if the LaoderItemObject is complete or not
		*  @param val true or false
		*/
		public function set completed(val:Boolean):void
		{
			_isCompleted = val;
		}
		
		/**
		*  The content loaded
		*  @param val The content loaded
		*/
		public function set content(val:Object):void
		{
			_content = val;
			_isCompleted = true;
			_percent = 1;
			
			// déclenchement event
			var evt:LoaderEvent = new LoaderEvent(LoaderEvent.COMPLETED);
			evt.percent = _percent;
			this.dispatchEvent(evt);
		}
		/**
		*  The content loaded
		*  @return  The content loaded
		*/
		public function get content():Object
		{
			return _content;
		}
		
		/**
		* Enable or disable the cache of the internet browser. Default false
		* @return true or false
		 */
		public function get cache():Boolean { return _cache; }
		
		/**
		* Enable or disable the cache of the internet browser. Default false
		* @param value true or false
		 */
		public function set cache(value:Boolean):void 
		{
			_cache = value;
		}
		
		
		/**
		* Create an instance of LoaderItemObject
		* @param pid An unique id (string) for LoaderItemObject		* @param purl The url of the object to load
		* @param ptype The type of the object to load. If no set, the type is determinated with the file extension. The valid type are enumerated in LoaderItemType
		* @param pcontext Indicate if the object will be loaded in the context of the application. Default true
		* @param plabel The label for LoaderItemObject. Default null
		*/
		public function LoaderItemObject(pid:String = null, purl:String = null, ptype:String = null,  pcontext:Boolean = true, plabel:String = null ):void
		{
			this._isCompleted = false;
			this._percent = 0;
			
			this.id = pid;
			this.url = purl;
			this.type = ptype;
			this.context = pcontext;
			this.label = plabel;
			
		}
		/**
		 * Remove and clear internal properties of the LoaderItemObject
		 */
		public function dispose():void
		{
			this._isCompleted = false;
			this._percent = 0;
			_content = null;
			
		}
	}

}
